import vue from "../main.js";
import config from "./config.js";

let http = {};
http.request = function({ method = "GET", url, data, loadingStatus = true }) {
  return new Promise((resolve, reject) => {
    if (loadingStatus) {
      uni.showLoading({ title: "Loading.." });
    }
    uni.request({
      url: config.BASEURL + url,
      method: method,
      data: data,
      header: {
        "content-type": "application/json",
        token: uni.getStorageSync("USER_INFO").token || "",
        way: "app"
      },
      dataType: "json",
      fail: res => { 
		uni.hideLoading();
        let ret = new Object();
        ret.code = 0;
        ret.msg = res.errMsg;
        reject(ret);
      },
      complete: res => {
		  uni.hideLoading();
        if (res.statusCode === 200) {
          //成功
          resolve(res.data);
        } else if (res.statusCode === 401) {
          uni.removeStorageSync("USER_INFO");
          uni.reLaunch({ url: "/pages/login/login" });
        } else {
          let ret = new Object();
          ret.code = 0;
          ret.msg = res.errMsg;
          reject(ret);
        }
      }
    });
  });
};

export { http };
